<?php
class login extends Controller {

    function login() {
        parent::Controller();
        if ($this->session->userdata('admin_logged_in')) redirect(base_url().'mk/admin/representatives/newrepresentative', 'refresh');
        $this->load->model('administrators');
        $this->load->library('session');
        $this->load->helper('url');
    }

   function index() {

        $user = $this->input->post('username');
        $pass = $this->input->post('password');
        $data['error_message'] = '';
        if ($this->session->userdata('admin_logged_in')) redirect(base_url().'mk/admin/representatives/newrepresentative', 'refresh');
        if (isset($_POST['login'])) {
            $this->load->model('administrators');
            $result = $this->administrators->checkAdmin($user, $pass);
            if ($result) {
                $admin_session = array(
                   'adminname'  => $result[0]->admin_name,
                   'admin_id'     => $result[0]->admin_id,
                   'admin_logged_in' => TRUE
               );

                $this->session->set_userdata($admin_session);
                redirect(base_url().'mk/admin/representatives/newrepresentative', 'refresh');
            }
            else $data['error_message'] = "Wrong ! Please try again!";
        }
        $this->load->view('admin/admin_login',$data);
    }

    function logout() {
        $admin_session = array(
                   'adminname'  => '',
                   'admin_id'     => '',
                   'admin_logged_in' => ''
               );
        $this->session->unset_userdata($admin_session);
        redirect(base_url().'login', 'refresh');
    }
    
}

?>
